const { src, dest, series, parallel, watch } = require('gulp');
const less = require('gulp-less');
const gulp = require('gulp');
const minifyCSS = require('gulp-csso'); //把css代码优化
const concat = require('gulp-concat');  //合并代码
const autoprefixer = require('gulp-autoprefixer');  //自动补全浏览器的前缀
const babel = require('gulp-babel');  //将es6转换成es5
const uglify = require('gulp-uglify'); //压缩js文件
const htmlMinify = require("gulp-html-minify"); //压缩html文件
const smushit = require('gulp-smushit'); //压缩图片
const livereload = require('gulp-livereload'); //修改代码自动编译
const connect = require('gulp-connect'); // 全自动
const {createProxyMiddleware} = require('http-proxy-middleware')

var browserify = require('browserify');
var babelify = require('babelify');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var standalonify = require('standalonify');

let distPath = 'dist/';
// let distPath = '../../src/main/resources/static/';

function css() {
    // src你要操作的文件夹的位置，* 是通配符表示src/css文件夹下的所有以.css结尾的文件
    return src('src/less/*.less')
        .pipe(less())
        .pipe(dest('src/css'))
    //dest是指你输出文件的位置，如果你的项目内没有dist文件夹，将会帮你自动生成这个文件夹
        .pipe(connect.reload());
}

function cssMinify() {
    return src('src/css/*.css')
        .pipe(concat('all.min.css'))
        .pipe(autoprefixer({
            cascade: true, //是否美化属性值 默认：true 像这样
            remove: true //是否去掉不必要的前缀 默认：true
        }))
        .pipe(minifyCSS())
        .pipe(dest(distPath + "css"))
        .pipe(connect.reload());
}


function config(){
    const env = process.env.NODE_ENV || 'fast'
	const config = 'src/config/application-'+env+'.yml';

    return gulp.src(config)
    	.pipe(concat('application.yml')) 
        // .pipe(babel())
        // .pipe(uglify({mangle: false, compress: {properties: false}, output: {quote_keys: true}}))
        .pipe(gulp.dest("dist/config"));
}


function js(){
    // return src(['src/js/**/*.js'])
    return src(['src/js/fastDevelop.js'])
    .pipe(concat('fastDevelop.js'))
    .pipe(babel({
        presets: ['@babel/env']
    }))
    .pipe(uglify())
    .pipe(dest(distPath + 'js'))
    .pipe(connect.reload());
}


function module(){
    return src('src/js/module/**')
        .pipe(dest(distPath + 'js/module'))
}

function compoment(){
    return src('src/compoment/**')
        .pipe(dest(distPath + 'compoment'))
}

function html(){
    return src('src/*.html')
    // .pipe(htmlMinify())
    .pipe(dest(distPath))
    .pipe(connect.reload());
}

function image(){
    return src('src/image/**')
    // .pipe(smushit())
        .pipe(dest(distPath + 'image'))
        .pipe(connect.reload());
}



function amis(){
    return src([
        './node_modules/amis/sdk/**'
    ])
    .pipe(dest(distPath + 'compoment/amis'))
}


function jqgrid(){
    return src([
        './node_modules/pqgridf/**'

    ])
    .pipe(dest(distPath + 'compoment/jqgrid'))
}

function layui(){
    return src([
        './node_modules/layui/**'

    ])
    .pipe(dest(distPath + 'compoment/layui'))
}

function ztree(){
    return src([
        './node_modules/@ztree/ztree_v3/**',
        '!./node_modules/@ztree/ztree_v3/css/zTreeStyle/img/zTreeStandard.png'

    ])
    .pipe(dest(distPath + 'compoment/@ztree/ztree_v3'))
}


function jquery_ui(){
    return src([
        './node_modules/jquery-ui-dist/**'

    ])
    .pipe(dest(distPath + 'compoment/jquery-ui'))
}


function jquery(){
    return src([
        './node_modules/jquery/dist/*'

    ])
    .pipe(dest(distPath + 'compoment/jquery'))
}


function ajax_hook(){
    return src([
        './node_modules/ajax-hook/**'

    ])
    .pipe(dest(distPath + 'compoment/ajax-hook'))
}

// function fast_config(){
//     return src([
//         'src/config/*'

//     ])
//     .pipe(dest(distPath + 'config'))
// }

function fast_data() {
    return src('src/data/**')
        .pipe(dest(distPath + 'data'))
}



function watches(){
    livereload.listen();
    watch(['src/less/*.less'], series(css,cssMinify))
    watch(['src/css/*.css'], series(cssMinify))
    watch(['src/js/*.js'], parallel(js))
    watch(['src/js/module/**'], parallel(module))
    watch(['src/index.html'], parallel(html))
    watch(['src/image/**'], parallel(image))
    watch(['src/env/*'], parallel(env))
    watch(['src/compoment/**'], parallel(compoment))
    watch(['src/data/**'], parallel(fast_data))

}

function server(){
    connect.server({
        root: 'dist/',  //再dist文件夹下查找资源
        port: 8888, //浏览器内打开的端口号
        livereload: true, //是否实时刷新

        middleware: function (connect, opt) {
            return [
                createProxyMiddleware(
                    '/platform',{
                        target: 'http://11.11.37.36:30110'
                    }),
                createProxyMiddleware(
                    '/builder',{
                        // target: 'http://11.2.251.48:8211'
                        target: 'http://11.2.251.48:8210'
                    }),
                createProxyMiddleware(
                  '/api', {
                        target: 'http://localhost:8080'
                    })
            ]
        }
    })
    watch(['src/less/*.less'], series(css,cssMinify))
    watch(['src/css/*.css'], series(cssMinify))
    watch(['src/js/*.js'], parallel(js))
    watch(['src/js/module/**'], parallel(module))
    watch(['src/index.html'], parallel(html))
    watch(['src/image/**'], parallel(image))
    watch(['src/config/*'], parallel(config))
    watch(['src/compoment/**'], parallel(compoment))
    watch(['src/data/**'], parallel(fast_data))

}



exports.css = css;
exports.cssMin = series(css, cssMinify); //series是让这两个任务串行执行
exports.js = js
exports.html = html
exports.image = image
exports.default = parallel(this.cssMin,js,html,amis,jqgrid,jquery_ui,jquery,layui,ztree,module,fast_data,compoment,image,ajax_hook,config)
exports.watch = series(this.default,watches)
exports.server = series(this.default,server)



